/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package siswa;

import java.awt.Color;
import java.awt.Dimension;
import java.beans.PropertyVetoException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.JDesktopPane;
import javax.swing.JOptionPane;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeSelectionModel;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author Rendra
 */
public class SiswaTree extends javax.swing.JInternalFrame {

    private String destPath;
            
    /**
     * Creates new form SiswaTree
     */
    public SiswaTree() {
        initComponents();
        jTabbedPane1.setSelectedIndex(1);
        makeTree();
    }
    
    private void makeTree() {
        destPath = App.common.getWorkDir();
        TreeSiswa.setRootVisible(true);
        TreeSiswa.setPreferredSize(new Dimension(200, TreeSiswa.getHeight()));
        
        Document doc = null;
        String filename = destPath+"/siswa.xml";
        
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setValidating(false);  // Not important fro this demo

            DocumentBuilder db = dbf.newDocumentBuilder();
            doc = db.parse(filename);
        } catch( FileNotFoundException fnfEx ) {
        } catch( Exception ex ) {
        }
        
        Node root = (Node)doc.getDocumentElement();
        DefaultMutableTreeNode top = createTreeNode(root, false);
        DefaultTreeModel dtModel = new DefaultTreeModel(top);
        TreeSiswa.setModel(dtModel);        
        TreeSiswa.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        TreeSiswa.setShowsRootHandles(true);
        TreeSiswa.setEditable(false);
        
        // Expand all node of JTree
        for (int i=0;i<TreeSiswa.getRowCount();i++) {  
            TreeSiswa.expandRow(i);  
        }
    }
    
    protected DefaultMutableTreeNode createTreeNode( Node root, boolean showDetails ) {
        DefaultMutableTreeNode dmtNode;

        String type = getNodeType(root);
        String name = root.getNodeName();
        String value = root.getNodeValue();

        if( showDetails ) {
            dmtNode = new DefaultMutableTreeNode("["+type+"] --> "+name+" = "+value);
        } else {
            dmtNode = new DefaultMutableTreeNode( root.getNodeType() == Node.TEXT_NODE ? value : name );
        }

        NamedNodeMap attribs = root.getAttributes();
        if (attribs != null && showDetails ) {
            for( int i = 0; i < attribs.getLength(); i++ ) {
                Node attNode = attribs.item(i);
                String attName = attNode.getNodeName().trim();
                String attValue = attNode.getNodeValue().trim();

                if (attValue != null) {
                    if (attValue.length() > 0) {
                        dmtNode.add(new DefaultMutableTreeNode("[Attribute] --> "+attName+"=\""+attValue+"\"") );
                    }
                }
            }
        }

        if (root.hasChildNodes()) {
            NodeList childNodes = root.getChildNodes();
            if(childNodes != null) {
                for (int k=0; k<childNodes.getLength(); k++) {
                    Node nd = childNodes.item(k);
                    if ( nd != null ) {
                        if ( nd.getNodeType() == Node.ELEMENT_NODE ) {
                            dmtNode.add(createTreeNode(nd, showDetails));
                        }

                        String data = nd.getNodeValue();
                        if (data != null) {
                            data = data.trim();
                            if (!data.equals("\n") && !data.equals("\r\n") && data.length() > 0) {
                                dmtNode.add(createTreeNode(nd, showDetails));
                            }
                        }
                    }
                }
            }
        }
        return dmtNode;
    }

    public String getNodeType( Node node ) {
        String type;
        
        switch ( node.getNodeType() ) {
            case Node.ELEMENT_NODE: {
                type = "Element";
                break;
            }
            case Node.ATTRIBUTE_NODE: {
                type = "Attribute";
                break;
            }
            case Node.TEXT_NODE: {
                type = "Text";
                break;
            }
            case Node.CDATA_SECTION_NODE: {
                type = "CData section";
                break;
            }
            case Node.ENTITY_REFERENCE_NODE: {
                type = "Entity reference";
                break;
            }
            case Node.ENTITY_NODE: {
                type = "Entity";
                break;
            }
            case Node.PROCESSING_INSTRUCTION_NODE: {
                type = "Processing instruction";
                break;
            }
            case Node.COMMENT_NODE: {
                type = "Comment";
                break;
            }
            case Node.DOCUMENT_NODE: {
                type = "Document";
                break;
            }
            case Node.DOCUMENT_TYPE_NODE: {
                type = "Document type";
                break;
            }
            case Node.DOCUMENT_FRAGMENT_NODE: {
                type = "Document fragment";
                break;
            }
            case Node.NOTATION_NODE: {
                type = "Notation";
                break;
            }
            default: {
                type = "Unknown, contact Sun!";
                break;
            }
        }
        return type;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        BtnKembali = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        TreeSiswa = new javax.swing.JTree();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextPane1 = new javax.swing.JTextPane();
        jPanel2 = new javax.swing.JPanel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        N_UN = new javax.swing.JTextField();
        N_KET = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        N_JK = new javax.swing.JComboBox();
        N_PEK = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        N_GAJI = new javax.swing.JComboBox();
        BtnProses = new javax.swing.JButton();

        BtnKembali.setText("<< Kembali");
        BtnKembali.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnKembaliActionPerformed(evt);
            }
        });

        jScrollPane1.setViewportView(TreeSiswa);

        jScrollPane2.setViewportView(jTextPane1);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 301, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 304, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane1.addTab("Aturan", jPanel3);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setText("Pengujian Sederhana");

        jLabel2.setText("Nama Lengkap Siswa :");

        jLabel3.setText("Nilai UN :");

        jLabel4.setText("Keterampilan :");

        jLabel5.setText("Jenis Kelamin :");

        jLabel6.setText("Pekerjaan Wali :");

        N_JK.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Laki-Laki", "Perempuan" }));

        N_PEK.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "PNS", "Karyawan Swasta", "Lainnya" }));

        jLabel7.setText("Gaji Wali :");

        N_GAJI.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "<= Rp 1.250.000", "Rp 1.250.001 s/d Rp 2.500.000", "Rp 2.500.001 s/d Rp 5.000.000", ">= Rp 5.000.001" }));

        BtnProses.setText("Proses Data");
        BtnProses.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnProsesActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(7, 7, 7)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jSeparator1)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jLabel3)
                                    .addComponent(N_JK, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(N_UN)
                                    .addComponent(jLabel5)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                                        .addComponent(jLabel7)
                                        .addGap(42, 42, 42)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(N_KET)
                                    .addGroup(jPanel2Layout.createSequentialGroup()
                                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel4)
                                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                                .addComponent(N_PEK, javax.swing.GroupLayout.Alignment.LEADING, 0, 90, Short.MAX_VALUE)
                                                .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.LEADING)))
                                        .addGap(0, 0, Short.MAX_VALUE))))
                            .addComponent(jTextField1)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(N_GAJI, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(BtnProses))
                                .addGap(0, 110, Short.MAX_VALUE)))))
                .addGap(8, 8, 8))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(N_UN, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(N_KET, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(26, 26, 26)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(N_JK, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(N_PEK, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(N_GAJI, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(BtnProses)
                .addContainerGap(63, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Pengujian", jPanel2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(BtnKembali)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTabbedPane1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTabbedPane1)
                    .addComponent(jScrollPane1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(BtnKembali)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BtnKembaliActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnKembaliActionPerformed
        // TODO add your handling code here:
        JDesktopPane desk = this.getDesktopPane();
        desk.removeAll();
        desk.repaint();
        LoadDataset frm = new LoadDataset();
        desk.add(frm);
        frm.setVisible(true);
        frm.setBorder(null);
        try {
            frm.setMaximum(true);
            ((javax.swing.plaf.basic.BasicInternalFrameUI)frm.getUI()).setNorthPane(null);
            frm.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.LIGHT_GRAY));
        } catch (PropertyVetoException pvx) {
            Logger.getLogger(Siswa.class.getName()).log(Level.SEVERE, null, pvx);
        }
    }//GEN-LAST:event_BtnKembaliActionPerformed

    private void BtnProsesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnProsesActionPerformed
        float t_un      = Float.parseFloat(N_UN.getText());
        float t_ket     = Float.parseFloat(N_KET.getText());
        float t_gaji    = Float.parseFloat(N_GAJI.getSelectedItem().toString());
        
        String t_pek    = N_PEK.getSelectedItem().toString();
        String v_kel    = N_JK.getSelectedItem().toString();
        String v_un     = "";
        String v_ket    = "";
        String v_pek    = "";
        String v_gaji   = "";
        
        /**
         * Konversi menjadi parameter perhitungan lanjut
         */
        if (t_un > 35.00 && t_un <= 40.00)
            v_un = "A";
        else if (t_un > 30.00 && t_un <= 35.00)
            v_un = "B";
        else 
            v_un = "C";
        
        if (t_ket > 9.00 && t_ket <= 10.00)
            v_ket = "A";
        else if (t_ket > 8.00 && t_ket <= 9.00)
            v_ket = "B";
        else 
            v_ket = "C";
        
        switch (t_pek) {
            case "PNS":
                v_pek = "A";
                break;
            case "Karyawan Swasta":
                v_pek = "B";
                break;
            default:
                v_pek = "C";
                break;
        }
        
        if (t_gaji <= 1250000)
            v_gaji = "A";
        else if (t_gaji > 1250000 && t_gaji <= 2500000)
            v_gaji = "B";
        else if (t_gaji > 2500000 && t_gaji <= 5000000) 
            v_gaji = "C";
        else
            v_gaji = "D";
        
        try {
            // TODO add your handling code here:
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(new File(destPath+"/siswa.xml"));
            Node n = doc.getFirstChild();
            NodeList nl = n.getChildNodes();
            Node an, an2;
            
            for (int i=0; i < nl.getLength(); i++) {
                an = nl.item(i);
                if (an.getNodeType()==Node.ELEMENT_NODE) {
                    NodeList nl2 = an.getChildNodes();
                    System.out.println(an.getAttributes().getNamedItem("decide_on").getNodeValue());
                    for(int i2=0; i2<nl2.getLength(); i2++) {
                        an2 = nl2.item(i2);
                        // DEBUG PRINTS
                        
                        /**
                        System.out.println(an2.getNodeName() + ": type (" + an2.getNodeType() + "):");
                        if(an2.hasChildNodes()) System.out.println(an2.getFirstChild().getTextContent());
                        if(an2.hasChildNodes()) System.out.println(an2.getFirstChild().getNodeValue());
                        System.out.println(an2.getTextContent());
                        System.out.println(an2.getNodeValue());
                        System.out.println(an2.getNodeName());
                        */
                    }
                }
            }
            
            String[] objektif = {"SMA", "SMK"};
            int idx = new Random().nextInt(objektif.length);
            JOptionPane.showMessageDialog(null, objektif[idx]);
            
            System.out.println("UN : "+v_un);
            System.out.println("Keterampilan : "+v_ket);
            System.out.println("Gender : "+v_kel);
            System.out.println("Pekerjaan : "+v_pek);
            System.out.println("Gaji : "+v_gaji);
            
        } catch (ParserConfigurationException | SAXException | IOException ex) {
            Logger.getLogger(SiswaTree.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_BtnProsesActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BtnKembali;
    private javax.swing.JButton BtnProses;
    private javax.swing.JComboBox N_GAJI;
    private javax.swing.JComboBox N_JK;
    private javax.swing.JTextField N_KET;
    private javax.swing.JComboBox N_PEK;
    private javax.swing.JTextField N_UN;
    private javax.swing.JTree TreeSiswa;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextPane jTextPane1;
    // End of variables declaration//GEN-END:variables
}
